using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using API;
using Grpc.Core;
using mb;
using Mb;
using Xamarin.Forms;

namespace libhot
{

	public class HomePage : ContentPage
	{
		public HomePage()
		{
			this.EnableNavBar(false);

			var hrm = CatLib.App.Make<HotReloadManager>();
			hrm.RegisterReload(this, "home", (resName) =>
			{
				App.logger.Log("homepage reload");
				this.Content = null;
				this.LoadXamlFromRes(resName);
				this.DoLoad();
			});
		}

		protected async void DoLoad()
		{
			await Task.Delay(10);
			// TestVideo(this, EventArgs.Empty);
			// TestSlider(this, EventArgs.Empty);
			// TestGetType(this, EventArgs.Empty);
		}

		void TestGetType(object sender, EventArgs arg)
		{
			var t1 = Type.GetType("mb.App");
			var t2 = Type.GetType("mb.iOS.AppDelegate");
			App.logger.Log("find type:{0},{1}", t1, t2);
		}

		void TestNullable(object sender, EventArgs arg)
		{
			int k = Test(1, 2);
			App.logger.Log("test:{0}", k);
			TestGetType(sender, arg);
		}
		int Test(int a, int b)
		{
			int c = 10;
			var keep = this.FindByName<Syncfusion.XForms.Buttons.SfCheckBox>("KeepPlaying").IsChecked;
			bool add = keep == true;
			return add ? a + b + c : a * b;
		}

		void TestList(object sender, EventArgs arg)
		{
			App.Instance.MainPage.Navigation.PushAsync(new TestListPage());
		}
		void TestGC(object sender, EventArgs arg)
		{
			App.Instance.MainPage.Navigation.PushAsync(new TestGcPage());
		}
		void TestSlider(object sender, EventArgs arg)
		{
			App.Instance.MainPage.Navigation.PushAsync(new TestSliderPage());
		}

		void TestVideo(object sender, EventArgs arg)
		{
			GC.Collect();
			GC.WaitForPendingFinalizers();
			// new mb.TestObject();
			// new HotTestObject();

			App.Instance.MainPage.Navigation.PushAsync(new VideoPage());
		}

		void TestPop(object sender, EventArgs arg)
		{

			var pm = CatLib.App.Make<IPopManager>();
			pm.ShowWaitMessage("点一下就消失", "测试", (pop) =>
			{
				pop.Close();
				pm.ShowToast("点了");
			});
		}

		async void TestHttp(object sender, EventArgs arg)
		{
			App.logger.Log("---test http");
			var pm = CatLib.App.Make<IPopManager>();
			pm.ShowToast("http测试中");

			string post = null;
			var (code, res) = await my.Http.MakeRequest(new my.Http.Option()
			{
				Url = "http://www.baidu.com",
				Method = post == null ? HttpMethod.Get : HttpMethod.Post,
				Body = post,
				OnProgress = (now, total) =>
				{
					// SetProgress(1.0f * now / total);
				},
				OnFinish = (ok, statu, msg) =>
				{
					App.logger.Log("download finish1:{0},{1}", statu, msg);
				}

			});
			App.logger.Log("download finish2:{0},{1}", code, res);
		}

	}
}